package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.personal.SettingsActivity;
import com.zhongjian.technologycal.mall.activity.web.WebViewMarkActivity;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.dialog.ColorDialog;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;

import butterknife.BindView;
import butterknife.OnClick;
import me.drakeet.materialdialog.MaterialDialog;

/**
 * Created by wei on 2017/11/16.
 * 客服中心
 *
 */

public class ServiceCenterActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    private int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1003;
    private MaterialDialog materialDialog = null;

    @Override
    protected void beforeInit() {
        super.beforeInit();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
//        StatusBarUtil.setTranslucent(this, 0);
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
//        StatusBarUtil.setColor(this, getResources().getColor(R.color.border_color), 0);
        tv_main_title.setText("客服中心");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_service_center;
    }
    @OnClick(R.id.iv_back_page)
    public void onClickBack(){
        finish();
    }

    /**
     * 保障说明
     */
    @OnClick(R.id.center_ll_explain)
    public void onClickCenterExplain(){
        Intent intent = new Intent();
        intent.setClass(getBaseContext(), WebViewMarkActivity.class);
        intent.putExtra("url", BaseContacts.URL_ABOUT_EXPLAIN);
        startActivity(intent);
    }

    /**
     * 交易规则
     */
    @OnClick(R.id.center_ll_tansaction)
    public void onClickCenterTansaction(){
        Intent intent = new Intent();
        intent.setClass(getBaseContext(), WebViewMarkActivity.class);
        intent.putExtra("url", BaseContacts.URL_ABOUT_PAY);
        startActivity(intent);

    }

    /**
     * 反馈建议
     */
    @OnClick(R.id.center_ll_fadeback)
    public void onClickCenterFadeback(){
        Intent intent = new Intent();
        intent.setClass(getBaseContext(), SuggestionActivity.class);
        startActivity(intent);
    }

    /**
     * 客服电话
     */
    @OnClick(R.id.center_ll_phone)
    public void onClickCenterPhone(){
        if (ContextCompat.checkSelfPermission(ServiceCenterActivity.this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
            // 没有获得授权，申请授权
            if (ActivityCompat.shouldShowRequestPermissionRationale(ServiceCenterActivity.this,
                    Manifest.permission.CALL_PHONE)) {
                // 返回值：
//                          如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
//                          如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
//                          如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
                // 弹窗需要解释为何需要该权限，再次请求授权
                ToastUitl.showShort("请授权");

                // 帮跳转到该应用的设置界面，让用户手动授权
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            }else{
                // 不需要解释为何需要该权限，直接请求授权
                ActivityCompat.requestPermissions(ServiceCenterActivity.this,
                        new String[]{Manifest.permission.CALL_PHONE},
                        1003);
            }
        }else {
            // 已经获得授权，可以打电话

          callPhone();

        }


    }

    /**
     * 拨打电话
     */
    private void callPhone(){
        materialDialog = new MaterialDialog(ServiceCenterActivity.this)
                .setTitle("客服电话")
                .setMessage("0571-57183790")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:0571-57183790"));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        materialDialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        materialDialog.dismiss();
                    }
                });
        materialDialog.setCanceledOnTouchOutside(true);
        materialDialog.show();
    }

    @Override
    public void initPresenter() {

    }

    // 处理权限申请的回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1003: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 授权成功，继续打电话
                    callPhone();
                } else {
                    // 授权失败！
                    ToastUitl.showShort("授权失败");
                }
                break;
            }
        }
    }


}
